* Estimates for analysis section of paper
* New file for version 3, August 2024

*Run this block if stand alone. If commented out, it is because this will be run from _ricardo_master.do
*set more off, perm
*clear
*graph drop _all
*cd  "C:/Users/tokay/Nouveau Dossier/Dropbox/HRR_InflDisaster/Ricardo" // 
*local dir "C:\Users\PILIPENT\Dropbox\HRR_InflDisaster\Ricardo"  // 


local updateIteration "2024_11Nov"

///////////////////////////////// Graph style //////////////////////////////////
    //  ssc install grstyle
    set scheme s2color
    grstyle init

    grstyle color background white
    // gridlines
    grstyle color major_grid dimgray
    grstyle linewidth major_grid thin
    grstyle yesno draw_major_hgrid yes
    grstyle yesno grid_draw_min yes
    grstyle yesno grid_draw_max yes
    grstyle anglestyle vertical_tick horizontal grstyle color ci_area gs12%50
    // legend
    grstyle clockdir legend_position 6
    grstyle numstyle legend_cols 2
    grstyle linestyle legend none
////////////////////////////////////////////////////////////////////////////////


******************************
* US and EZ 5y5y for introduction -- note need internet access for Fred
******************************
clear
* EZ data
	import excel "$home_parent/DataFromBloomberg/Bloomberg_FWISEU55_`updateIteration'.xlsx", sheet("Sheet1") cellrange(A6) firstrow clear
	rename PX_LAST swap5y5y
	rename Dates date
	g m = month(date)
	g y = year(date)
	collapse (mean) swap5y5y, by(y m)
	g date = ym(y,m)
	format date %tm
	drop y m 
	save "$input/EZ_5y5y", replace 
* US data 
	set fredkey 4b647bee15904cd531180baf48767ea4
	import fred T5YIFR, daterange(2010-01-01 .) aggregate(monthly,avg) clear
	rename daten date
	g m = month(date)
	g y = year(date)
	replace date = ym(y,m)
	format date %tm
	drop y m dates
	save "$input/US_5y5y", replace
* Merge the two
	merge 1:1 date using "$input/EZ_5y5y"
	drop _merge
	rename T5YIFR US5y5y
	rename swap5y5y EZ5y5y
* Figure
	tsset date
	keep if date>=ym(2011,1)
	tsline US5y5y EZ5y5y, ///
	lp(solid solid) lw(thick thick) ytitle("%") xtitle("Month") ylabel(0(1)5) tlabel(2011m1(24)2024m12, angle(45)) ///
	legend(label(1 "United States") label(2 "Eurozone") position(2) ring(0)) ///
	name(Fives)
	graph export "$figures/figv3_5y5y.pdf", replace

	
******************************
* Estimates of model parameters
******************************
*US
clear
import excel "$input/parameters_mdl101month.xlsx", sheet("US_mdl101_month") firstrow clear
generate date_ym = tm(2011m1) + _n -1
format %tm date_ym
tsset date_ym
*keep if date_ym>=ym(2011,1)

	graph twoway tsline pi_nL pi_nH pi_nn, ///
	lp(solid dash dash_dot) lw(thick thick thick) ytitle("Probability") xtitle("Month") ylabel(0(0.05)0.4) tlabel(2011m1(24)2024m12) ///
	legend(label(1 "p{subscript:dl}") label(2 "p{subscript:dh}") label(3 "p{subscript:nn}") position(2) ring(0) row(1)) ///
	name(modelUS)
	graph export "$figures/figv3_model101USmonth.pdf", replace

*EZ
clear
import excel "$input/parameters_mdl101month.xlsx", sheet("EZ_mdl101_month") firstrow clear
generate date_ym = tm(2011m1) + _n -1
format %tm date_ym
tsset date_ym
*keep if date_ym>=ym(2011,1)

	graph twoway tsline pi_nL pi_nH pi_nn, ///
	lp(solid dash dash_dot) lw(thick thick thick) ytitle("Probability") xtitle("Month") ylabel(0(0.05)0.4) tlabel(2011m1(24)2024m12) ///
	legend(label(1 "p{subscript:dl}") label(2 "p{subscript:dh}") label(3 "p{subscript:nn}") position(2) ring(0) row(1)) ///
	name(modelEZ)
	graph export "$figures/figv3_model101EZmonth.pdf", replace


******************************
* US - deflation 2011-14
******************************
clear
u "$input/US_allm"
tsset date_ym
keep if date_ym>=ym(2011,1)
keep if date_ym<ym(2015,1)

  graph twoway tsline p0_5y5y_low_c pm1_5y5y_low_c, ///
  lp(solid dash) lw(thick thick ) ytitle("Probability") xtitle("Month") ylabel(0(0.05)0.25) tlabel(2010m12(6)2014m12) ///
  legend(label(1 "Deflation (<0%)") label(2 "Serious deflation (<-1%)")  position(2) ring(0) row(1)) ///
  name(UStail02)
  graph export "$figures/figv3_US_def.pdf", replace
    
  graph twoway tsline p0_5y5y_pool_c pm1_5y5y_pool_c, ///
  lp(solid dash) lw(thick thick ) ytitle("Probability") xtitle("Month") ylabel(0(0.05)0.25) tlabel(2010m12(6)2014m12) ///
  legend(label(1 "Deflation (<0%)") label(2 "Serious deflation (<-1%)")  position(2) ring(0) row(1)) ///
  name(UStail03)
  graph export "$figures/figv3_US_def_appxpool.pdf", replace
******************************
* EZ - deflation 2010-19
******************************
clear
u "$input/EZ_allm"
tsset date_ym
keep if date_ym>=ym(2011,1)

  graph twoway tsline p0_5y5y_low_c pm1_5y5y_low_c p0_5y_low_c, ///
  lp(solid dash dash_dot) lw(thick thick thick) ytitle("Probability") xtitle("Month") ylabel(0(0.05)0.25) tlabel(2011m1(24)2024m12) ///
  legend(label(1 "Deflation") label(2 "Serious deflation") label(3 "5-year deflation")  position(2) ring(0) row(1)) ///
  name(EZtail01)
  graph export "$figures/figv3_EZ_def.pdf", replace


  graph twoway tsline p0_5y5y_pool_c pm1_5y5y_pool_c p0_5y_pool_c, ///
  lp(solid dash dash_dot) lw(thick thick thick) ytitle("Probability") xtitle("Month") ylabel(0(0.05)0.25) tlabel(2011m1(24)2024m12) ///
  legend(label(1 "Deflation") label(2 "Serious deflation") label(3 "5-year deflation")  position(2) ring(0) row(1)) ///
  name(EZtail02)
  graph export "$figures/figv3_EZ_def_appxpool.pdf", replace


******************************
* Pandemic
******************************
clear
u "$input/US_allm"
rename p4_5y5y_high_c USp4_5y5y_high_c
rename p4_10y_high_c USp4_10y_high_c 
rename p4_5y_high_c USp4_5y_high_c
*keep date_ym USp4_5y5y_high_c
merge 1:1 date_ym using "$input/EZ_allm"
rename p4_5y5y_high_c EZp4_5y5y_high_c
rename p4_5y_high_c EZp4_5y_high_c
rename p4_10y_high_c EZp4_10y_high_c
*keep date_ym USp4_5y5y_high_c EZp4_5y5y_high_c

tsset date_ym
keep if date_ym>=ym(2020,1)

	*Baseline
	graph twoway tsline USp4_5y5y_high_c EZp4_5y5y_high_c, ///
	lp(solid dash) lw(thick thick ) ytitle("Probability") xtitle("Month") ylabel(0(0.03)0.15) tlabel(2020m1(12)2024m6) ///
	legend(label(1 "United States") label(2 "Eurozone")  position(10) ring(0) row(1)) ///
	name(pandemic, replace)
	graph export "$figures/figv3_pandemic.pdf", replace

	*5 years
  	graph twoway tsline USp4_5y_high_c EZp4_5y_high_c, ///
	lp(solid dash) lw(thick thick) ytitle("Probability") xtitle("Month") ylabel(0(0.06)0.36) tlabel(2020m1(12)2024m6) ///
	legend(label(1 "United States") label(2 "Eurozone")  position(10) ring(0) row(1)) ///
	name(pandemic5, replace)
	graph export "$figures/figv3_pandemic5y.pdf", replace


	// Joint EZ and US, should be in figure_wesbite, but pain there because of renaming of variables
	
******************************
* Pandemic densities
******************************

* US-pandemic case
clear
u "$input/US_dist_Z_monthly"

g temp1=100*frequency if date_stata==mdy(3,4,2020) & dist_identifier=="ZQ10"
g temp2=100*frequency if date_stata==mdy(3,3,2021) & dist_identifier=="ZQ10"
g temp3=100*frequency if date_stata==mdy(3,8,2022) & dist_identifier=="ZQ10"
g temp4=100*frequency if date_stata==mdy(3,2,2023) & dist_identifier=="ZQ10"
g temp5=100*frequency if date_stata==mdy(3,27,2024) & dist_identifier=="ZQ10"

label variable temp1	"Mar 2020"
label variable temp2	"Mar 2021"
label variable temp3	"Mar 2022"
label variable temp4	"Mar 2023"
label variable temp5	"Mar 2024"

graph twoway mspline temp1 support if support>-0.01 & support<0.06,  lp(solid solid) lw(thick thick) ytitle("%") xtitle("Inflation") xlabel(-0.01(0.01)0.06) ylabel(0(5)50) legend(label(1 "Mar 2020") position(1) ring(0)) ///
|| mspline temp2 support  if support>-0.01 & support<0.06, lp(solid solid) lw(thick thick) ytitle("%") xtitle("Inflation") xlabel(-0.01(0.01)0.06) ylabel(0(5)50) legend(label(2 "Mar 2021") position(1) ring(0)) ///
|| mspline temp3 support  if support>-0.01 & support<0.06, lp(solid solid) lw(thick thick) ytitle("%") xtitle("Inflation") xlabel(-0.01(0.01)0.06) ylabel(0(5)50) legend(label(3 "Mar 2022") position(1) ring(0)) ///
|| mspline temp4 support  if support>-0.01 & support<0.06, lp(solid solid) lw(thick thick) ytitle("%") xtitle("Inflation") xlabel(-0.01(0.01)0.06) ylabel(0(5)50) legend(label(4 "Mar 2023") position(1) ring(0)) ///
|| mspline temp5 support  if support>-0.01 & support<0.06, lp(solid solid) lw(thick thick) ytitle("%") xtitle("Inflation") xlabel(-0.01(0.01)0.06) ylabel(0(5)50) legend(label(5 "Mar 2024") position(1) ring(0)) ///
name(USsmoothpandhist)
graph export "$figures/figv3_US_densities.pdf", replace

* EZ-pandemic case
clear
u "$input/EZ_dist_Z_monthly"

g temp1=100*frequency if date_stata==mdy(3,4,2020) & dist_identifier=="ZQ10"
g temp2=100*frequency if date_stata==mdy(3,5,2021) & dist_identifier=="ZQ10"
g temp3=100*frequency if date_stata==mdy(3,8,2022) & dist_identifier=="ZQ10"
g temp4=100*frequency if date_stata==mdy(3,2,2023) & dist_identifier=="ZQ10"
g temp5=100*frequency if date_stata==mdy(3,19,2024) & dist_identifier=="ZQ10"

label variable temp1	"Mar 2020"
label variable temp2	"Mar 2021"
label variable temp3	"Mar 2022"
label variable temp4	"Mar 2023"
label variable temp5	"Mar 2024"

graph twoway mspline temp1 support if support>-0.01 & support<0.06,  lp(solid solid) lw(thick thick) ytitle("%") xtitle("Inflation") xlabel(-0.01(0.01)0.06) ylabel(0(5)50) legend(label(1 "Mar 2020") position(1) ring(0)) ///
|| mspline temp2 support  if support>-0.01 & support<0.06, lp(solid solid) lw(thick thick) ytitle("%") xtitle("Inflation") xlabel(-0.01(0.01)0.06) ylabel(0(5)50) legend(label(2 "Mar 2021") position(1) ring(0)) ///
|| mspline temp3 support  if support>-0.01 & support<0.06, lp(solid solid) lw(thick thick) ytitle("%") xtitle("Inflation") xlabel(-0.01(0.01)0.06) ylabel(0(5)50) legend(label(3 "Mar 2022") position(1) ring(0)) ///
|| mspline temp4 support  if support>-0.01 & support<0.06, lp(solid solid) lw(thick thick) ytitle("%") xtitle("Inflation") xlabel(-0.01(0.01)0.06) ylabel(0(5)50) legend(label(4 "Mar 2023") position(1) ring(0)) ///
|| mspline temp5 support  if support>-0.01 & support<0.06, lp(solid solid) lw(thick thick) ytitle("%") xtitle("Inflation") xlabel(-0.01(0.01)0.06) ylabel(0(5)50) legend(label(5 "Mar 2024") position(1) ring(0)) ///
name(EZsmoothpandhist)
graph export "$figures/figv3_EZ_densities.pdf", replace


	
******************************
* Effect of initial conditions on disaster probabilities for EZ and US
******************************

******* US
clear
import excel "$input/Initial_US.xlsx", sheet("Bins") firstrow  clear
gen date=ym(year,month)
format date %tm
* Risk factor correction
	replace Baseline=Baseline*0.662
	forvalues i=1/8{	
		replace Bin`i'=Bin`i'*0.662
	}

drop if date <ym(2022,1)
	graph twoway connected Baseline Bin5 Bin7 date, ///
	lp(solid dash dash_dot) lw(thick thick thick) ytitle("Probability") xtitle("Month") ylabel(0(0.03)0.15) tlabel(2022m1(6)2025m1) ///
	legend(label( 1 "Baseline") label(2 "(2,3]") label(3 "(4,5]") position(1) ring(0) row(1)) ///
	name(USinitial, replace)
	graph export "$figures/figv3_US_initial.pdf",replace

******* EZ
clear
import excel "$input/Initial_EZ.xlsx", sheet("Bins") firstrow  clear
gen date=ym(year,month)
format date %tm
* Risk factor correction
	replace Baseline=Baseline*0.662
	forvalues i=1/8{	
		replace Bin`i'=Bin`i'*0.662
	}

drop if date <ym(2022,1)
	graph twoway connected Baseline Bin5 Bin7 date, ///
	lp(solid dash dash_dot) lw(thick thick thick) ytitle("Probability") xtitle("Month") ylabel(0(0.03)0.15) tlabel(2022m1(6)2025m1) ///
	legend(label( 1 "Baseline") label(2 "(2,3]") label(3 "(4,5]") position(1) ring(0) row(1)) ///
	name(EZinitial, replace)
	graph export "$figures/figv3_EZ_initial.pdf",replace


******************************
* Conditional probabilities
******************************

******* US
clear
import excel "$input/Conditional_US_new.xlsx", sheet("Sheet1") firstrow clear
format Date %tm
* Corrected Risk Factor 
	replace Year0=Year0*0.662
	replace Year1=Year1*0.662
	replace Year2=Year2*0.662

drop if Date <ym(2022,1)
	graph twoway connected Year0 Year1 Year2 Date, ///
	lp(solid dash dash_dot) lw(thick thick thick) ytitle("Probability") xtitle("Month") ylabel(0(0.03)0.15) tlabel(2022m1(6)2025m1) ///
	legend(label( 1 "Baseline") label(2 "1-year") label(3 "2-year") position(1) ring(0) row(1)) ///
	name(USconditional, replace)
	graph export "$figures/figv3_US_conditional.pdf",replace

******* EZ
clear
import excel "$input/Conditional_EZ_new.xlsx", sheet("Sheet1") firstrow clear
format Date %tm
* Corrected Risk Factor 
	replace Year0=Year0*0.662
	replace Year1=Year1*0.662
	replace Year2=Year2*0.662

drop if Date <ym(2022,1)
	graph twoway connected Year0 Year1 Year2 Date, ///
	lp(solid dash dash_dot) lw(thick thick thick) ytitle("Probability") xtitle("Month") ylabel(0(0.03)0.15) tlabel(2022m1(6)2025m1) ///
	legend(label( 1 "Baseline") label(2 "1-year") label(3 "2-year") position(1) ring(0) row(1)) ///
	name(EZconditional, replace)
	graph export "$figures/figv3_EZ_conditional.pdf",replace

******************************
* Standard errors
******************************
clear
u "$input/dataSEs"
format date_ym %tm
tsset date_ym
keep if date_ym>=ym(2011,1)

*** Plot
	graph twoway (rarea Pareto_US_lo Pareto_US_hi date_ym, color(%60)), ytitle("Probability") xtitle("Month") ylabel(0(0.03)0.15) name(SEpareto)
	graph export "$figures/figv3_SEpareto.pdf", replace

keep if date_ym>=ym(2020,1)
	graph twoway (rarea joint_US_lo joint_US_hi date_ym, color(%60)), ytitle("Probability") xtitle("Month") ylabel(0(0.03)0.15)	name(SEjoint)
	graph export "$figures/figv3_SEjoint.pdf", replace
